﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CLVisionMES
{
    [Serializable]
    internal class DataGridItemStruct : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _key;
        public string Key
        {
            get { return _key; }
            set
            {
                _key = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Key)));
            }
        }
        private object _value;
        public object Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
                }
            }
        }

        private EnumDataType _type;
        public EnumDataType Type
        {
            get { return _type; }
            set
            {
                if (_type != value)
                {
                    _type = value;
                    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Type)));
                }
            }
        }

        public int HeaderIndex { get; set; }

        //如果是数组元素，其索引值
        public int ArrayIndex { get; set; }

        private ObservableCollection<object> _valueList;
        //存储 值下拉菜单元素
        public ObservableCollection<object> ValueList
        {
            get { return _valueList; }
            set
            {
                _valueList = value;
            }
        }

        //存储 类型下拉菜单元素
        public ObservableCollection<EnumDataType> TypeList { get; set; }
        public DataGridComment DataGridComments { get; set; }

        public DataGridItemStruct()
        {
            TypeList = new ObservableCollection<EnumDataType>();
            ValueList = new ObservableCollection<object>();
            DataGridComments = new DataGridComment();

            ValueList.CollectionChanged += (s, e) => {
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ValueList)));
            };
            //默认值 -1，不是数组元素
            ArrayIndex = -1;
        }

        public DataGridItemStruct Clone()
        {
            // 创建一个新的 DataGridItemStruct 对象
            var cloned = new DataGridItemStruct
            {
                Key = this.Key,
                Value = this.Value,
                Type = this.Type,
                HeaderIndex = this.HeaderIndex,
                ArrayIndex = this.ArrayIndex,
                ValueList = new ObservableCollection<object>(this.ValueList),
                TypeList = new ObservableCollection<EnumDataType>(this.TypeList)
            };

            // 递归克隆 Comment 属性
            if (this.DataGridComments != null)
            {
                cloned.DataGridComments = this.DataGridComments.Clone();
            }

            return cloned;
        }
    }
}
